package net;

import java.net.InetSocketAddress;
import java.nio.ByteOrder;
import java.util.concurrent.Executors;

import org.jboss.netty.bootstrap.ClientBootstrap;
import org.jboss.netty.bootstrap.ServerBootstrap;
import org.jboss.netty.buffer.HeapChannelBufferFactory;
import org.jboss.netty.channel.socket.nio.NioClientSocketChannelFactory;
import org.jboss.netty.channel.socket.nio.NioServerSocketChannelFactory;

import system.Config;

public class Net {

	private final int port;
	
	public ServerBootstrap serverBootstrap; 
	
	public ClientBootstrap clientBootstrap; 
	

	public Net(int port) {
		this.port = port;
		clientBootstrap = new ClientBootstrap(
                new NioClientSocketChannelFactory(
                        Executors.newCachedThreadPool(),
                        Executors.newCachedThreadPool()));
        // Set up the event pipeline factory.
		clientBootstrap.setPipelineFactory(new HttpSnoopClientPipelineFactory());
		
		serverBootstrap = new ServerBootstrap(
				new NioServerSocketChannelFactory(
						Executors.newCachedThreadPool(),
						Executors.newCachedThreadPool()));
		serverBootstrap.setPipelineFactory(new ServerPipelineFactory());
		serverBootstrap.setOption("child.bufferFactory",
				new HeapChannelBufferFactory(ByteOrder.LITTLE_ENDIAN));
		serverBootstrap.setOption("child.tcpNoDelay", true);
		serverBootstrap.setOption("child.keepAlive", true);
		serverBootstrap.setOption("child.TIMEOUT", Config.NET_TIME_OUT);
	}

	public void run() {
        // Configure the client.
		serverBootstrap.bind(new InetSocketAddress(port));
	}

}
